package com.zealousbd.adserver.webapp.i18n;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.ListResourceBundle;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zealousbd.adserver.Constants;

public class ApplicationResources extends ListResourceBundle {

	public ApplicationResources() {
		super();
	}

	protected final transient Log log = LogFactory
			.getLog(ApplicationResources.class);

	@Override
	protected Object[][] getContents() {
		ClassLoader cl = getClass().getClassLoader();
		Properties props = new Properties();
		String resource = Constants.APPLICATION_RESOURCES_LOCATION
				+ getClass().getSimpleName() + ".xml";

		log.error(resource);
		URL url = cl.getResource(resource);
		try {
			 InputStream is = new FileInputStream(url.getPath());
			 props.loadFromXML(is);
			 is.close();
		} catch (IOException e) {
			log.error(e.getMessage());
		}

		 Enumeration<Object> keys = props.keys();
		ArrayList<Object[]> properties = new ArrayList<Object[]>();
		
		 for (int i = 0; keys.hasMoreElements(); i++) {
			String key = (String) keys.nextElement();
			String value = props.getProperty(key);
			properties.add(new Object[] { key, value });
			log.debug("key=" + key + " value=" + value);
		}
		return properties.toArray(new Object[][] {});

	}
}
